package com.cloudea.lotus.utils;

import com.cloudea.lotus.Server;
import com.google.gson.JsonObject;
import com.mysql.cj.log.Log;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Map;

public class StringUtil {
    private static Logger logger = Logger.getLogger(StringUtil.class);
    private static final String ENCODING = "utf-8";

    public static String toString(ResultSet resultSet, String column, int type){
        try {
            if(type == Types.SMALLINT){
                return String.valueOf(resultSet.getShort(column));
            }else if(type == Types.INTEGER){
                return String.valueOf(resultSet.getInt(column));
            }else if(type == Types.BIGINT){
                return String.valueOf(resultSet.getLong(column));
            }else if(type == Types.FLOAT){
                return String.valueOf(resultSet.getFloat(column));
            }else if(type == Types.DOUBLE || type == Types.DECIMAL){
                return String.valueOf(resultSet.getDouble(column));
            }else if(type == Types.CHAR || type == Types.VARCHAR || type == Types.LONGVARCHAR){
                return String.valueOf(resultSet.getString(column));
            }else if(type == Types.NCHAR || type == Types.NVARCHAR || type == Types.LONGNVARCHAR){
                return String.valueOf(resultSet.getNString(column));
            }else if(type == Types.TIMESTAMP ||  type == Types.TIMESTAMP_WITH_TIMEZONE){
                Timestamp timestamp = resultSet.getTimestamp(column);
                if(timestamp != null){
                    return String.valueOf(timestamp.getTime());
                }
            }
        }catch (Exception throwables){
            logger.trace("类型转换失败", throwables);
        }
        return "";
    }

    public static void toColumn(PreparedStatement statement, int index, String value, int type){
            try {
                if(type == Types.SMALLINT){
                    statement.setShort(index, Short.parseShort(value));
                }else if(type == Types.INTEGER){
                    statement.setInt(index, Integer.parseInt(value));
                }else if(type == Types.BIGINT){
                    statement.setLong(index, Long.parseLong(value));
                }else if(type == Types.FLOAT){
                    statement.setFloat(index, Float.parseFloat(value));
                }else if(type == Types.DOUBLE || type == Types.DECIMAL){
                    statement.setDouble(index, Double.parseDouble(value));
                }else if(type == Types.CHAR || type == Types.VARCHAR || type == Types.LONGVARCHAR){
                    statement.setString(index, value);
                }else if(type == Types.NCHAR || type == Types.NVARCHAR || type == Types.LONGNVARCHAR){
                    statement.setNString(index, value);
                }else if(type == Types.TIMESTAMP ||  type == Types.TIMESTAMP_WITH_TIMEZONE){
                    statement.setTimestamp(index, new Timestamp(Long.parseLong(value)));
                }
            } catch (Exception throwables) {
                logger.trace("类型转换失败", throwables);
            }
    }

    public static String toTip(String method, String path, Map<String, String> queryParameters){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(method);
        stringBuilder.append(": ");
        stringBuilder.append(path);
        if(queryParameters != null){
            stringBuilder.append("?");
            int index = 0;
            for(String key : queryParameters.keySet()){
                stringBuilder.append(key + "=" + queryParameters.get(key));
                if(index != queryParameters.size() - 1){
                    stringBuilder.append("&");
                }
                index += 1;
            }
        }
        return stringBuilder.toString();
    }

    public static String streamToString(InputStream inputStream) throws Exception{
        byte[] allBytes = inputStream.readAllBytes();
        inputStream.close();
        return new String(allBytes,  ENCODING);
    }

}
